@include ('user.header', ['title' => '添加短网址'])

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container">
        <ol class="breadcrumb mb-0">
            <li class="breadcrumb-item"><a href="./">用户中心</a></li>
            <li class="breadcrumb-item"><a href="/user/dwz-list/{{$domain['id']}}">短网址列表[{{$domain['domain']}}]</a></li>
            <li class="breadcrumb-item active" aria-current="page">添加短网址</li>
        </ol>
    </div>
</nav>

<div class="container py-4">
    <div class="row">
        <div class="col-12 col-md-8 mx-auto">
            @if (isset($result))
                @if ($result['status'] == '0')
                    <div class="alert alert-warning">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        {{ $result['msg'] }}
                    </div>
                @else
                    <div class="alert alert-success">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        {{ $result['msg'] }}
                    </div>
                @endif
            @endif
            @if(isset($domain))
            @if($domain['status'] == 1)
            <div class="card shadow-sm mb-3">
                <div class="card-body p-3">
                    <form id="addDomainForm" action="{{ url('/api/dwz/add/'.$domain['id']) }}" method="post" class="needs-validation" novalidate>
                        @csrf
                        <input type="hidden" name="api_token" value="{{ session('user')->api_token }}">

                        <div class="mb-3">
                            <label for="domain" class="form-label fw-bold">域名前缀</label>
                            <input type="text" name="domain" id="domain" value="{{$domain['domain']}}" class="form-control" disabled required>
                        </div>

                        <div class="mb-3">
                            <label for="url" class="form-label fw-bold">跳转地址</label>
                            <input type="url" name="url" id="url" value="{{ old('url') }}" class="form-control" placeholder="如：wwww.mengyunblog.com" required>
                        </div>

                        <div class="mb-3">
                            <label for="code" class="form-label fw-bold">域名后缀</label>
                            <input type="text" name="code" id="code" value="{{ old('code') }}" class="form-control" placeholder="自定义长度，不填则随机生成">
                        </div>

                        <div class="d-grid gap-2 mt-4">
                            <button type="submit" class="btn btn-primary btn-sm rounded-pill py-2 fs-6 shadow-sm">
                                <i class="bi bi-plus-circle me-1"></i>添加短网址
                            </button>
                        </div>
                    </form>
                </div>
            </div>
            @else
            <div class="alert alert-danger">
                <a href="/user/domain-list" class="close" data-dismiss="alert" aria-hidden="true">返回域名列表</a>
            该域名未审核或已被禁用！
            </div>
            @endif
            @else
            <div class="alert alert-danger">
                <a href="/user/domain-list" class="close" data-dismiss="alert" aria-hidden="true">返回域名列表</a>
            该域名不属于你，或者不存在！
            </div>
            @endif

            <script>
                // 提交表单时设置请求头
                $('#addDomainForm').submit(function(event) {
                    event.preventDefault();

                    var formData = $(this).serialize();

                    $.ajax({
                        url: "/api/dwz/add/{{$domain['id']}}",
                        type: "POST",
                        data: formData,
                        headers: {
                            'Authorization': 'Bearer {{ session('user')->api_token }}'
                        },
                        success: function(response) {
                            if (response.status == '1') {
                                alert('添加成功');
                                window.location.href = "/user/dwz-list/{{ $domain['id'] }}";
                            } else {
                                alert('添加失败: ' + response.msg);
                            }
                        },
                        error: function(response) {
                            alert('添加失败: ' + response.responseJSON.msg);
                        }
                    });
                });
            </script>
        </div>

        <div class="col-12 col-md-4">
            <div class="card shadow-sm mb-3">
                <div class="card-body p-3">
                    {{-- <p><span class="glyphicon glyphicon-info-sign"></span> 使用前，请先把域名解析到以下服务器中的任意一个服务器！</p> --}}
                    {{-- <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>记录类型</th>
                                <th>记录值</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- 服务器列表 -->
                        </tbody>
                    </table> --}}
                    {{-- <hr/> --}}
                    <b>跳转地址</b><br/>
                    跳转地址要带上http://或者https://的玩着域名<hr/>
                    <b>自定义后缀</b><br/>
                    填写xxx则短连接是{{ $domain['domain'] }}/xxx，不填则随机生成
                </div>
            </div>
        </div>
    </div>
</div>

@include ('user.footer')